class Stack {
    constructor() {
        this.s = []
    }
    // 入栈
    push(item) {
        this.s[this.s.length] = item
    }
    // 出栈
    pop() {
        if (this.s.length !== 0) {
            this.s = this.s.slice(0,-1)
        }
        else {
            throw new Error("栈中没有元素")
        }
    }
    // 返回栈顶元素
    peek() {
        return this.s[this.s.length - 1] ? this.s[this.s.length - 1] : null
    }
    // 判断栈是否为空
    empty() {
        return this.s.length === 0
    }
    // 清空栈
    clear() {
        this.s = []
    }
    // 返回给定对象的位置  应该是从顶到底算
    search(item) {
        let i = this.s.lastIndexOf(item)
        if (i >= 0) {
            return this.size() - i;
        } else {
            return -1;
        }
    }
    // 栈元素个数
    size() {
        return this.s.length
    }
    // 获取栈数据
    getData() {
        return [...this.s]
    }

}
let stack = new Stack()
stack.push("zhang")
stack.push("tang")
console.log(stack.getData());
console.log(stack.peek());
console.log(stack.size());
console.log(stack.search('tang'));
stack.pop()
console.log(stack.getData());
stack.clear()
console.log(stack.empty());
